package com.qianfeng.goods_7006.controller;

import com.qianfeng.entity.Goods;
import com.qianfeng.entity.Result;
import com.qianfeng.goods_7006.client.LoginClient;
import com.qianfeng.goods_7006.dto.GoodsDto;
import com.qianfeng.goods_7006.repository.GoodsRepository;
import com.qianfeng.goods_7006.service.GoodsService;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.data.elasticsearch.core.query.GetQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

/**
 * author: 田晋宇
 * datetime:2021/11/1 15:13
 * email:853796359@qq.com
 */
@RestController
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private GoodsRepository goodsRepository;

    @Autowired
    private ElasticsearchRestTemplate template;

    @RequestMapping("/test")
    public List<Goods> test(Integer tid){




        return goodsService.getGoodsByType(tid);
    }

    @RequestMapping("/init")
    public String init(){
        goodsService.init();
        return "ok";
    }

    @RequestMapping("/test1")
    public String test(){


        List<GoodsDto> list = goodsRepository.queryAllByName("良品铺子");
        System.out.println(list);
        return "ok";
    }
    @RequestMapping("/test2")
    public String test2(){


        /*List<GoodsDto> list = goodsRepository.queryAllByTaste("香辣");
        System.out.println(list);*/
        Pageable pageable= PageRequest.of(0,999);
        NativeSearchQueryBuilder builder=new NativeSearchQueryBuilder();
        NativeSearchQuery query=builder.withQuery(QueryBuilders.matchQuery("name", "良品铺子"))
                .withPageable(pageable)
                .build();

        SearchHits<GoodsDto> search1 = template.search(query,GoodsDto.class);
        search1.get().forEach(System.out::println);

        return "ok";


    }

    @RequestMapping("/test3")
    public String test3(){
        List<GoodsDto> goodsDtos = goodsRepository.queryAllByType(13);
        System.out.println(goodsDtos);
        return "ok";

    }

    @RequestMapping("/test4")
    public String test4(){
        List<GoodsDto> goodsDtos = goodsRepository.queryAllByPriceIsLessThan(10.0);
        System.out.println(goodsDtos);
        return "ok";

    }

}
